package com.example.newproject_overseas.Screen

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.newproject_overseas.MyApplication
import com.example.newproject_overseas.R
import com.example.newproject_overseas.compositionLocal.LocalUserViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first

@Composable
fun FristScreen(navController: NavHostController) {

    //将 注册好的数据 带过来 并且赋给名称LoginViewodel
    var loginVM = LocalUserViewModel.current
    MyApplication.loginVM = LocalUserViewModel.current


    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color(0XFFEFF0F3)),

        ) {

            Image(
                modifier = Modifier
                    .fillMaxSize()
                    .height(260.dp),
                contentScale = ContentScale.FillBounds,
                painter  = painterResource(
                    id = R.drawable.activate),
                    contentDescription = null,
                )
        }

        // Navigate to the login screen after a delay of 3 seconds
        LaunchedEffect(key1 = true) {
            delay(500) // Delay for 3 seconds
            //val userId = loginVM.localLoginUser.first()?.userId
            //Log.i("loginVM.localLoginUser", userId?:"")
            val localLoginUser = loginVM.localLoginUser

            if (localLoginUser != null) {
                navController.navigate("MainFrame")
            } else {
                navController.navigate("login")
            }
        }
}

@Preview
@Composable
fun FristScreenPreview() {
    val navController = rememberNavController() // 假设这里使用了导航控制器
    FristScreen(navController)
}

